<template>
  <div class="login">
    <div class="login-box">
      <div class="top">
        <div class="logo"><img src="https://xiaoxiangai.oss-cn-beijing.aliyuncs.com/2024/05/07/logo.e5facb4.png1"
               alt=""></div>
      </div>
      <div class="mid">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 status-icon>
          <el-form-item prop="userName">
            <el-input class="info"
                      v-model="dataForm.userName"
                      placeholder="帐号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input class="info"
                      v-model="dataForm.password"
                      type="password"
                      placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item prop="captcha">
            <el-row :gutter="20">
              <el-col :span="14">
                <el-input v-model="dataForm.captcha"
                          placeholder="验证码">
                </el-input>
              </el-col>
              <el-col :span="10"
                      class="login-captcha">
                <img :src="captchaPath"
                     @click="getCaptcha()"
                     alt="">
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <div class="item-btn">
              <input type="button"
                     value="登录"
                     @click="dataFormSubmit()" style="display: inline-block;width: 40%;margin:0;">
<!--              <span style="margin-right:20px;"><a href="/loginByMobile">短信登录</a></span>-->
            </div>
          </el-form-item>
          <el-form-item>
            <div class="form-footer">
            <!-- <el-checkbox v-model="savePasswotd">三天内自动登录</el-checkbox> -->
              <!--<span><a href="/registe">没有账号？请先注册</a></span>
            <span><a href="/resetPassword">忘记密码?</a></span>-->
            </div>
          </el-form-item>
        </el-form>
      </div>

<!--      <div class="bottom">Copyright © 2020 POS收银系统线上服务平台</div>-->
    </div>

  </div>
</template>

<script>
import { getUUID } from '@/utils'
export default {
  data () {
    return {
      savePasswotd:false,
      dataForm: {
        userName: '',
        password: '',
        uuid: '',
        captcha: ''
      },
      dataRule: {
        userName: [
          { required: true, message: '帐号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        captcha: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      captchaPath: ''
    }
  },
  created () {
    this.getCaptcha()
    // var userName = this.$cookie.get('userName')
    // var password = this.$cookie.get('password')
    // if (userName && password) {
    //   this.userName = userName
    //   this.password = password
    // }
		// credentials: "xeld2020", //密码
		// imageCode: "6fj3", //验证码
		// loginType: 1, //登录类型 1.賬號密碼登錄 2，手機號登錄，3用戶名登錄
		// principal: "admin", //賬號
		// sessionUUID: "f30891f9-e889-4f16-8c8d-afed02e07a00",  //隨機碼
		// t: 1610095780842, //時間戳
  },
  methods: {
    // 提交表单
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/login?grant_type=admin'),
            method: 'post',
            data: this.$http.adornData({
              'loginType': 1,
              'principal': this.dataForm.userName,
              'credentials': this.dataForm.password,
              'sessionUUID': this.dataForm.uuid,
              'imageCode': this.dataForm.captcha,
            })
          }).then(({ data }) => {
            if (this.savePasswotd) {
              this.$cookie.set('Authorization', 'bearer' + data.access_token,3)
              // this.$cookie.set('userName', this.data.userName,3)
              // this.$cookie.set('password', this.data.password,3)
            }else{
              this.$cookie.set('Authorization', 'bearer' + data.access_token)
            }
            this.$router.replace({ name: 'home' })
          }).catch(() => {
            this.getCaptcha()
          })
        }
      })
    },
    // 获取验证码
    getCaptcha () {
      this.dataForm.uuid = getUUID()
      this.captchaPath = encodeURI(this.$http.adornUrl(`/captcha.jpg?uuid=${this.dataForm.uuid}`))
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/img/login-bg.png) no-repeat;
  background-size: cover;
  position: fixed;
}
.login .login-box {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  height: 100%;
  padding-top: 10%;
}
.login .login-box .top {
  margin-bottom: 30px;
  text-align: center;
}
.login .login-box .top .logo {
  font-size: 0;
}
.login .login-box .top .company {
  font-size: 16px;
  margin-top: 10px;
}
.login .login-box .mid {
  font-size: 14px;
}
.login .login-box .mid .item-btn {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}
.login .login-box .mid .item-btn input {
  border: 0;
  width: 200px;
  height: 40px;
  box-shadow: 0;
  background: #1f87e8;
  color: #fff;
  border-radius: 3px;
}
.info {
  width: 410px;
}
.login-captcha {
  height: 40px;
}
.login .login-box .bottom {
  position: absolute;
  bottom: 10%;
  width: 100%;
  color: #999;
  font-size: 12px;
  text-align: center;
}
.form-footer{
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding-right: 20px;
  // font-size: 10px;
  color: #B7B8B7;
}
.el-checkbox__label {
  color: #B7B8B7;
}

.buy-box{
  background-color: #fff;
  height: 900px;
  width: 600px;
  position: absolute;
  left: 0;
  top: 0;
  img{
    width: 400px;
    margin: 40px 100px;
  }
  .el-button{
    margin-left: 50%;
    transform: translateX(-50%);
  }
}
</style>
